(function ($) {
    $.login = {
        formMessage: function (msg) {
            $('.tips_msg').html(msg);
        },
        loginClick: function () {
            var $account = $("#txt_account");
            var $password = $("#txt_password");
            var $code = $("#txt_code");
            if ($account.val() == "") {
            	$account.focus();
                $.login.formMessage('请输入登录帐号。');
                return false;
            } else if ($password.val() == "") {
                $password.focus();
                $.login.formMessage('请输入登录密码。');
                return false;
            } else if ($code.val() == "") {
                $code.focus();
                $.login.formMessage('请输入验证码。');
                return false;
            }
            else {
                $("#login_button").attr("disabled", true).text("loading...");
                $.ajax({
                    url: "login",
                    data: { account: $.trim($account.val()), password: $.trim($password.val()), captcha: $.trim($code.val()) },
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        if (data.result) {
                            $("#login_button").text("登录成功，正在跳转...");
                            window.setTimeout(function () {
                                window.location.href = "index.html";
                            }, 100);
                        } else {
                            $("#login_button").removeAttr('disabled').text("登录");
                            $("#imgcode").trigger("click");
                            $code.val('');
                            $.login.formMessage(data.message);
                        }
                    }
                });
            }
        },
        init: function () {
        	$(".container").addClass("animated bounceIn");
            $(".wrapper").height($(window).height());
            $(".container").css("margin-top", ($(window).height() - $(".container").outerHeight()) / 2);
            $(window).resize(function (e) {
                $('.wrapper').height($(window).height());
                $(".container").css("margin-top", ($(window).height() - $(".container").outerHeight()) / 2);
            });
            setTimeout(function(){
                $(".container").removeClass("bounceIn");
            }, 1000);
            $("#imgcode").click(function () {
                $(this).attr("src", "captcha?time=" + Math.random());
            });
            $("#login_button").click(function () {
                $.login.loginClick();
            });
            document.onkeydown = function (e) {
                if (!e) e = window.event;
                if ((e.keyCode || e.which) == 13) {
                    document.getElementById("login_button").focus();
                    document.getElementById("login_button").click();
                }
            };
        }
    };
    $(function () {
        $.login.init();
    });
})(jQuery);